(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cda.spa.spaViewController", spaViewController);
	spaViewController.$inject = [ "$q", "$scope", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService" ];
	function spaViewController($q, $scope, modalService, gridService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.spa = {};
		$scope.app.settings.spaPage = true;
		ctrl.init = function() {
			ctrl.spa.calendar = new Date();
			ctrl.spa.calendarTimesOpions = [ {
				"value" : 1,
				"label" : "option.day.times.one"
			},{
				"value" : 2,
				"label" : "option.day.times.two"
			},{
				"value" : 3,
				"label" : "option.day.times.three"
			} ];
			ctrl.spa.calendarTimes = 1;
			
			ctrl.spa.statistical = {};
			
			ctrl.spa.statistical.day_x = ["02:00", "04:00", "06:00", "08:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00", "22:00", "24:00"];
			
			ctrl.spa.statistical.day_data = [];
			angular.forEach(ctrl.spa.statistical.day_x, function(x){
				ctrl.spa.statistical.day_data[x] = new statisticalObj(getRandom(30), getRandom(30), getRandom(30), getRandom(30), getRandom(30));
			});
			
			ctrl.spa.statistical.day_data["04:00"] = new statisticalObj(0,0,21,11,13);
			ctrl.spa.statistical.day_data["14:00"] = new statisticalObj(0,0,21,0,13);
			
			
			angular.forEach(ctrl.spa.statistical.day_x, function(x){
				ctrl.spa.statistical.day_data_blank = ctrl.spa.statistical.day_data_blank || [];
				if(ctrl.spa.statistical.day_data[x]){
					ctrl.spa.statistical.day_data_blank[x] = processDay_data_blank_num(ctrl.spa.statistical.day_data[x]);
				}
			});
			
			ctrl.spa.statistical.totalType = "day";
			ctrl.spa.statistical.totalTypes = [{
				"value" : 'day',
				"label" : "option.statistical.type.day"
			},{
				"value" : 'week',
				"label" : "option.statistical.type.week"
			},{
				"value" : 'month',
				"label" : "option.statistical.type.month"
			},{
				"value" : 'year',
				"label" : "option.statistical.type.year"
			}];
			////////week
			ctrl.spa.statistical.statisticalTypes = [{
				"value" : 'total',
				"label" : "option.statistical.type.total",
				"order" : 1
			},{
				"value" : 'notification',
				"label" : "option.statistical.type.notification",
				"order" : 6
			},{
				"value" : 'message',
				"label" : "option.statistical.type.message",
				"order" : 5
			},{
				"value" : 'reminder',
				"label" : "option.statistical.type.reminder",
				"order" : 4
			},{
				"value" : 'task',
				"label" : "option.statistical.type.task",
				"order" : 3
			},{
				"value" : 'businessActivity',
				"label" : "option.statistical.type.businessactivity",
				"order" : 2
			}];
			ctrl.spa.statistical.statisticalType = "total";
			//test week
			ctrl.spa.statistical.statisticalData = [];
			ctrl.spa.statistical.statisticalData["week"] = [];
			angular.forEach(ctrl.spa.statistical.statisticalTypes, function(statisticalType){
				var unread = getRandom(200);
				ctrl.spa.statistical.statisticalData["week"][statisticalType.value] = {
					"unread" : getRandom(200),
					"total" : unread + getRandom(200)
				};
				
			});
		};
		
		ctrl.calendarTimesChanged = function(spa){
			console.log(spa.calendarTimes);
		};
		
		ctrl.statisticalTotalTypesClick = function(totalType){
			ctrl.spa.statistical.totalType = totalType.value;
			console.log(totalType);
		};
		
		ctrl.statisticalTypeClick = function(statisticalType){
			ctrl.spa.statistical.statisticalType = statisticalType.value;
		};
		
		ctrl.spaStatisticalDayDataClick = function(day_x, type, data){
			ctrl.spa.statistical.dayData = {
				"day_x": day_x,
				"type": type
			};
			console.log("day_x:" + day_x + ", $type:" + type + ", data:" + data);
		};
		
		function getRandom(num){
			return Math.ceil(num * Math.random());
		}
		
		function statisticalObj(notification, message, reminder, task, businessActivity){
			this.notification = notification;
			this.message = message;
			this.reminder = reminder;
			this.task = task;
			this.businessActivity = businessActivity;
		}
		
		function processDay_data_blank_num(day_datas){
			var num = 0;
			if(day_datas){
				angular.forEach(day_datas, function(data){
					if(data == 0){
						num++;
					}
				});
			}
			var result = [];
			for(var i = 0; i < num; i++){
				result[i] = new Object();
			}
			return result;
		}
	}
})(angular);
